package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xdialog.XDialogUpdateAPP;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.Specialty;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.release.ReleaseFM;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 五峰特产
 */
@Route(path = "/main/MainSpecialtyRUI")
public class MainSpecialtyRUI extends AbsRecyclerViewUI<Specialty> {
    public final static int REQUEST_OK = 0xff01;
    public final static int COLLECTION_RESULT = 0xff02;//收藏回调
    public final static int COMMENT_RESULT = 0xff03;//评论回调

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_main_specialty;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("五峰特产");
        showLoding();
    }
    
    @Override
    protected String setHttpPath() {
        return "Specialty/getSpecialtyList";
    }

    @Override
    protected List<Specialty> handleHttpData(JSONObject response) {
        List<Specialty> list = new ArrayList<Specialty>();
        if (200 == response.optInt("code")) {
            JSONArray listJson = response.optJSONArray("data");
            if (listJson != null && listJson.length() > 0) {
                for (int i = 0; i < listJson.length(); i++) {
                    JSONObject obj = listJson.optJSONObject(i);
                    Specialty model = new Specialty();
                    model.setThumb(obj.optString("thumb"));
                    model.setNid(obj.optString("id"));
                    model.setTitle(obj.optString("title"));
                    model.setContent(obj.optString("content"));
                    model.setStock(obj.optString("collection_count"));
                    model.setComment_count(obj.optString("comment_count"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected void handleHttpDataFailure() {
        super.handleHttpDataFailure();
        showLodingFail();
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_main_specialty;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, final int position, final Specialty model) {
        ((ExpandImageView) holder.getView(R.id.icon)).setImageURI(model.getThumb());
        holder.setText(R.id.title, model.getTitle());
        String content = model.getContent();
//        holder.setText(R.id.desc, content);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content,Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content)));
        }
        holder.setText(R.id.collectionNumber, model.getStock() + "收藏");
        holder.setText(R.id.commentNumber, model.getComment_count() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/main/MainSpecialtyInfoRUI")
                        .withString("type","2")
                        .withString("id", model.getNid())
                        .withInt("position", position)
                        .withString("collection_count", String.valueOf(model.getStock()))
                        .navigation(MainSpecialtyRUI.this,REQUEST_OK);
            }
        });
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_OK && (resultCode == COLLECTION_RESULT || resultCode == COMMENT_RESULT)) {
//            int position=data.getIntExtra("position",0);
//            Specialty model=getAdapter().getDatalist().get(position);
//            switch (resultCode) {
//                case COLLECTION_RESULT://收藏
//                    String collectionNumber=data.getStringExtra("collection_count");
//                    model.setStock(collectionNumber);
//                    break;
//                case COMMENT_RESULT://评论
//                    String commentNumber=data.getStringExtra("commentNumber");
//                    model.setCommentNumber(commentNumber);
//                    break;
//            }
//            getAdapter().notifyItemChanged(position);
//        }
//    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
